﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ipaidabribe.Code;

namespace ipaidabribe
{
    public partial class edit : System.Web.UI.Page
    {
        private int _bribeId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["sa"] == null)
            {
                Response.Redirect("/Admincp/Login.aspx");
            }

            if (Request.QueryString["id"] != null)
            {
                int id = 0;
                int.TryParse(Request.QueryString["id"].ToString(), out id);
                if (id > 0)
                {
                    _bribeId = id;
                    if (!Page.IsPostBack)
                    {
                        //lay du lieu 1 post
                        var db = new ipaidabribeEntities();
                        var bribe = db.Bribes.Where(t => t.Id == id).FirstOrDefault();
                        if (bribe != null)
                        {
                            //fill du lieu
                            lblTitle.Text = HttpUtility.HtmlDecode(bribe.Title.Trim());                            
                            lblContent.Text = HttpUtility.HtmlDecode(bribe.Content.Trim());
                            cbkEnable.Checked = Convert.ToBoolean(bribe.Enable);
                        }                                                
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {            
            var db = new ipaidabribeEntities();
            var bribe = db.Bribes.Where(t => t.Id == _bribeId).FirstOrDefault();
            if (bribe != null)
            {
                //update
                bribe.Title = HttpUtility.HtmlEncode(lblTitle.Text.Trim());
                bribe.Content = HttpUtility.HtmlEncode(lblContent.Text.Trim());
                bribe.Enable = cbkEnable.Checked;
                if(db.SaveChanges() >0)
                {
                    Response.Redirect("/view_reports_paid.aspx");
                }
            }     
        }
    }
}